﻿#define  _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>

/*
给你一个数组，将数组中的元素向右轮转 k 个位置，其中 k 是非负数。

示例 1:
输入: nums = [1,2,3,4,5,6,7], k = 3
输出: [5,6,7,1,2,3,4]
解释:
向右轮转 1 步: [7,1,2,3,4,5,6]
向右轮转 2 步: [6,7,1,2,3,4,5]
向右轮转 3 步: [5,6,7,1,2,3,4]
*/

/*
void rotat(int* nums, int numsSize, int k)
{
	//时间复杂度O(N*k)
	while (k--)
	{
		int tmp = nums[numsSize - 1];
		for (int end = numsSize - 2; end >= 0; --end)
		{
			nums[end + 1] = nums[end];
		}
		nums[0] = tmp;
	}
 }*/


//逆置
void Revers(int* nums, int left, int right)
{
	while (left < right)
	{
		int tmp = nums[left];
		nums[left] = nums[right];
		nums[right] = tmp;
		++left;
		--right;
	}
}

void rotat(int* nums, int numsSize, int k)
{
	if (k >= numsSize)
	{
		k %= numsSize;
	}
	Revers(nums, numsSize - k, numsSize - 1);
	Revers(nums, 0, numsSize - k - 1);
	Revers(nums, 0, numsSize- 1);
}

int main()
{
	int arr[] = { 1,2,3,4,5,6,7 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	int k = 0;
	scanf("%d", &k);
	rotat(arr, sz, k);
	for (int i = 0; i < sz; ++i)
	{
		printf("%d ", arr[i]);
	}
	return 0;
}